package cn.zhangxin.pethome.system.pet.service.impl;

import cn.zhangxin.pethome.base.query.BaseQuery;
import cn.zhangxin.pethome.constants.BaseConstants;
import cn.zhangxin.pethome.jwt.LoginContextUtils;
import cn.zhangxin.pethome.system.employee.domain.Employee;
import cn.zhangxin.pethome.system.employee.mapper.EmployeeMapper;
import cn.zhangxin.pethome.system.pet.domain.Pet;
import cn.zhangxin.pethome.system.pet.domain.PetDetail;
import cn.zhangxin.pethome.system.pet.domain.SearchMasterMsgAuditLog;
import cn.zhangxin.pethome.system.pet.mapper.PetDetailMapper;
import cn.zhangxin.pethome.system.pet.mapper.PetMapper;
import cn.zhangxin.pethome.system.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.zhangxin.pethome.system.pet.query.PetQuery;
import cn.zhangxin.pethome.system.pet.service.IPetService;
import cn.zhangxin.pethome.base.service.impl.BaseServiceImpl;
import cn.zhangxin.pethome.utils.AjaxResult;
import cn.zhangxin.pethome.utils.AuthService;
import cn.zhangxin.pethome.utils.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zx
 * @since 2023-02-06
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    private final String NOTE= "审核成功！";
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private AuthService authService;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Override
    public void add(Pet pet, HttpServletRequest request) {
        //获得登录用户
        Employee loginfo= (Employee)LoginContextUtils.getLoginUser(request);
        Employee username = employeeMapper.findOneByUsername(loginfo.getUsername());
        //设置所属店铺
        pet.setShopId(username.getShopId());
        //设置上架状态 添加默认是下架
        pet.setState(BaseConstants.PetState.STATE_DOWN);
        //设置上架时间
        pet.setCreatetime(new Date());
        //添加宠物
        petMapper.add(pet);

        //添加宠物表的详细信息
        Long id = pet.getId();
        PetDetail petDetail = pet.getDetail();
        petDetail.setPetId(id);
        petDetailMapper.add(petDetail);

    }


    public List<Pet> findByQuery(PetQuery petQuery, HttpServletRequest request) {

        //获得登录用户
        Employee loginfo= (Employee)LoginContextUtils.getLoginUser(request);
        Employee username = employeeMapper.findOneByUsername(loginfo.getUsername());
        petQuery.setShopId(username.getShopId());
        return petMapper.findByQuery(petQuery);
    }

    @Override
    public Long findByQueryCount(PetQuery petQuery, HttpServletRequest request) {
        //获得登录用户
        Employee loginfo= (Employee)LoginContextUtils.getLoginUser(request);
        Employee username = employeeMapper.findOneByUsername(loginfo.getUsername());
        petQuery.setShopId(username.getShopId());
        return petMapper.findByQueryCount(petQuery);
    }

    /* 上架 */
    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        //获得登录用户
        Employee loginfo= (Employee)LoginContextUtils.getLoginUser(request);
        Employee username = employeeMapper.findOneByUsername(loginfo.getUsername());
        //日志表

        StringBuilder stringBuilder = new StringBuilder();
        for (Long id : ids) {
            Pet one = petMapper.findOne(id);
            if (one.getState()==BaseConstants.PetState.STATE_DOWN && authService.authentication(one.getName())){
                one.setState(BaseConstants.PetState.STATE_ON);
                //设置上架时间
                one.setOnsaletime(new Date());
                petMapper.update(one);

                //补充日志表
                SearchMasterMsgAuditLog searchMasterMsgAuditLog = new SearchMasterMsgAuditLog();
                searchMasterMsgAuditLog.setAuditId(username.getId());
                searchMasterMsgAuditLog.setMsgId(331l);//设置死的消息
                searchMasterMsgAuditLog.setState(BaseConstants.PetState.STATE_SUCCEE);//设置日志状态
                searchMasterMsgAuditLog.setNote(one.getName()+NOTE);
                searchMasterMsgAuditLog.setAuditTime(new Date());
                searchMasterMsgAuditLogMapper.add(searchMasterMsgAuditLog);

            }else {
                stringBuilder.append(one.getName()+",");
            }
        }
        if (stringBuilder.length()>0){
           throw new BusinessException(stringBuilder.toString());
        }
    }
    //下架
    @Override
    public void offsale(List<Long> ids, HttpServletRequest request) {
        //获得登录用户
        Employee loginfo= (Employee)LoginContextUtils.getLoginUser(request);
        Employee username = employeeMapper.findOneByUsername(loginfo.getUsername());
        for (Long id : ids) {
            Pet one = petMapper.findOne(id);
            if (one.getState()==BaseConstants.PetState.STATE_ON){
                one.setState(BaseConstants.PetState.STATE_DOWN);
                //设置上架时间
                one.setOffsaletime(new Date());
                petMapper.update(one);

                //补充日志表
                SearchMasterMsgAuditLog searchMasterMsgAuditLog = new SearchMasterMsgAuditLog();
                searchMasterMsgAuditLog.setAuditId(username.getId());
                searchMasterMsgAuditLog.setMsgId(331l);//设置死的消息
                searchMasterMsgAuditLog.setState(BaseConstants.PetState.STATE_SUCCEE);//设置日志状态
                searchMasterMsgAuditLog.setNote(one.getName()+NOTE);
                searchMasterMsgAuditLog.setAuditTime(new Date());
                searchMasterMsgAuditLogMapper.add(searchMasterMsgAuditLog);

            }
        }
    }


    //删除
    @Override
    public void delete(Long id) {
        Pet one = petMapper.findOne(id);
        if (one.getState()==BaseConstants.PetState.STATE_DOWN){
            petDetailMapper.deleteByPetId(id);
            petMapper.delete(id);
        }else {
            throw new BusinessException("商铺处于上架不能删除！");
        }
    }
}
